using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using VSSDKAssist.Assets.Helpers;
using EnvDTE;
using VSSDKAssist.Assets.Actions.Base;
using System.Globalization;

namespace VSSDKAssist.Recipes.ConfigureVSPlk
{
	/// <summary>
	/// Action that adds/update the ProvideLoadKey attribute
	/// </summary>
	public class AddProvideLoadKeyAttributeAction : ActionBase
	{
		private CodeClass codeClass;

		/// <summary>
		/// Gets or sets the code class.
		/// </summary>
		/// <value>The code class.</value>
		[Input(Required = true)]
		public CodeClass CodeClass
		{
			get { return codeClass; }
			set { codeClass = value; }
		}

		private string pkgCompanyName;

		/// <summary>
		/// Gets or sets the name of the PKG company.
		/// </summary>
		/// <value>The name of the PKG company.</value>
		[Input(Required = true)]
		public string PkgCompanyName
		{
			get { return pkgCompanyName; }
			set { pkgCompanyName = value; }
		}

		private string packageName;

		/// <summary>
		/// Gets or sets the name of the package.
		/// </summary>
		/// <value>The name of the package.</value>
		[Input(Required = true)]
		public string PackageName
		{
			get { return packageName; }
			set { packageName = value; }
		}

		private string packageVersion;

		/// <summary>
		/// Gets or sets the package version.
		/// </summary>
		/// <value>The package version.</value>
		[Input(Required = true)]
		public string PackageVersion
		{
			get { return packageVersion; }
			set { packageVersion = value; }
		}

		private string vsEdition;

		/// <summary>
		/// Gets or sets the VS edition.
		/// </summary>
		/// <value>The VS edition.</value>
		[Input(Required = true)]
		public string VSEdition
		{
			get { return vsEdition; }
			set { vsEdition = value; }
		}

		private string plkResourceId;

		/// <summary>
		/// Gets or sets the PLK resource id.
		/// </summary>
		/// <value>The PLK resource id.</value>
		[Input(Required = true)]
		public string PlkResourceId
		{
			get { return plkResourceId; }
			set { plkResourceId = value; }
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			string attName = "ProvideLoadKey";
			string attValue =
				string.Format(
					CultureInfo.CurrentCulture,
					"\"{0}\", \"{1}\", \"{2}\", \"{3}\", {4}",
					this.VSEdition,
					this.PackageVersion,
					this.PackageName,
					this.PkgCompanyName,
					this.PlkResourceId);

			CodeAttribute provideLoadKeyAtt = FileCodeModelHelper.GetAttribute(this.codeClass, attName);

			if(provideLoadKeyAtt != null)
			{
				FileCodeModelHelper.UpdateAttribute(provideLoadKeyAtt, attValue);
			}
			else
			{
				FileCodeModelHelper.AddAttribute(codeClass, attName, attValue, false);
			}
		}
	}
}